﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ProductManagement;

public partial class Modules_Purchase_ucProductList : System.Web.UI.UserControl
{

    Products _products = new Products();

    protected void Page_Load(object sender, EventArgs e)
    {
        btnAdd.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnAdd.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnSearch.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSearch.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");      
        btnEdit.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnEdit.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnDelete.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnDelete.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");       

        if (!IsPostBack)
        {
            Session["Product_SearchProduct"] = string.Empty;
            Search();
        }
    }



    void Search()
    {
        try
        {
            DataSet dsProducts;
            string criteria = string.Empty;
            if (rblCriteria.SelectedIndex != -1)
                criteria = rblCriteria.SelectedItem.ToString();
            _products.SearchProduct(criteria,Session["Product_SearchProduct"].ToString(), out dsProducts);
            gvwProducts.DataSource = dsProducts;
            gvwProducts.Columns[1].Visible = true;
            gvwProducts.DataBind();
            gvwProducts.Columns[1].Visible = false;
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Search Product Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }

    void Delete()
    {
       
         try
         {  
            int index = 0;
            string[] productID = new string[gvwProducts.Rows.Count];
            
            for (int i = 0; i < gvwProducts.Rows.Count; i++)
            {
                CheckBox chkGrid = (CheckBox)(gvwProducts.Rows[i].Cells[0].FindControl("chkEditDelete"));
                if (chkGrid.Checked == true)
                {
                    productID[index] = gvwProducts.Rows[i].Cells[1].Text;
                    index++;
                }
            }


            string conditionDeleteProduct = "ProductID='" + productID[0] + "'";
            string conditionDeleteProductSupplierList = "ProductID='" + productID[0] + "'";
            string conditionDeleteProductAccessory = "ProductID='" + productID[0] + "'";
                        
            for (int i = 1; i < index; i++)
            {
                conditionDeleteProduct = conditionDeleteProduct + "or ProductID='" + productID[i] + "'";
                conditionDeleteProductSupplierList = conditionDeleteProductSupplierList + "or ProductID='" + productID[i] + "'";
                conditionDeleteProductAccessory = conditionDeleteProductAccessory + "or ProductID='" + productID[i] + "'";
            }

            _products.DeleteProduct(conditionDeleteProduct, conditionDeleteProductSupplierList, conditionDeleteProductAccessory);
            Response.Write("<script>alert('Deleted Successfully !')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Delete Products Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
       
    }
   

    void GetIDs()
    {        
        try
        {
            int count = 0;           
            string[] ID = new string[gvwProducts.Rows.Count];

            for (int i = 0; i < gvwProducts.Rows.Count; i++)
            {
                CheckBox chkGrid = (CheckBox)(gvwProducts.Rows[i].Cells[0].FindControl("chkEditDelete"));
                if (chkGrid.Checked == true)
                {
                    ID[count] = gvwProducts.Rows[i].Cells[1].Text;
                    count++;
                }
            }          

            Session["Product_IDs"] = ID;
            Session["Product_selectedProducts"] = count;
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Get ID Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

   

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Session["Product_btnAddAccessory"] = "false";
        Session["Product_btnAddSupplier"] = "false";
        Session["Product_selectedProducts"] = 0;
        Session["Product_Add/EditProduct"] = "Add";
        Session["UserManager_Control"] = "Production/ucAddProduct.ascx";    
        Response.Redirect("BusinessProcessAutomation.aspx");
       
    }
   
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["Product_SearchProduct"] = txtSearch.Text;
        Search();
    }


    protected void btnEdit_Click(object sender, EventArgs e)
    {
        Session["Product_FromControl"] = "Production/ucProductList.ascx";
        GetIDs();
        int countProducts = (int)Session["Product_selectedProducts"];
        Session["Product_btnAddAccessory"] = "false";
        Session["Product_btnAddSupplier"] = "false";
       
        Session["Product_Add/EditProduct"] = "Edit";
        if (countProducts > 1) // if more than 1 product is selected,go to add accessory page directly
        {            
            Session["UserManager_Control"] = "Production/ucProductAccessory.ascx";
        }
        else
            Session["UserManager_Control"] = "Production/ucAddProduct.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Delete();
        Search();
    }
    protected void btnAccessories_Click(object sender, EventArgs e)
    {
        GetIDs();
        Session["UserManager_Control"] = "Production/ucAccessoriesList.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void btnSuppliers_Click(object sender, EventArgs e)
    {

        GetIDs();
        Session["Product_module"] = "Products";
        Session["UserManager_Control"] = "Production/ucVendorList.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void gvwProducts_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        }
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";

        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }
    protected void gvwProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwProducts.PageIndex = e.NewPageIndex;
        Search();
    }
}
 